package de.koller.renderEngine.physics;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

public class Cube {

	private final World world;
	public final Body body;
	public final float width, height;
	
	public static FixtureDef createDefaultFixtureDef( Vec2 size, boolean box ) {
		FixtureDef f = new FixtureDef();
		f.density = 2f;
		f.friction = 2f;
		f.restitution = 0;
		
		if( box ) {
			PolygonShape p = new PolygonShape();
			p.setAsBox( size.x / 2f, size.y / 2f );
			f.shape = p;		
		} else {
			CircleShape p = new CircleShape();
			p.m_radius = size.x / 2f;
			f.shape = p;		
		}
		
		return f;
	}
	
	public static BodyDef createDefaultBodyDef( Vec2 pos, boolean s, float r ) {
		BodyDef b = new BodyDef();
		b.position = pos;
		b.angle = (float)Math.toRadians( r );
		if( s )
			b.type = BodyType.STATIC;
		else
			b.type = BodyType.DYNAMIC;
		return b;
	}
	
	public Cube( Vec2 pos, Vec2 size, World world, float r, boolean s, boolean box ) {
		this( createDefaultBodyDef( pos, s, r ), createDefaultFixtureDef( size, box ), world, size.x, size.y );
	}
	
	public Cube( BodyDef bodyDef, FixtureDef fixtureDef, World world, float width, float height ) {
		this.world = world;
		this.width = width;
		this.height = height;
		
		body = world.createBody( bodyDef );
		body.createFixture( fixtureDef );
		body.setUserData( this );
	}
	
}
